<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('选举任务列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>标题：</label>
                            <input type="text" name="title"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.addFull()" shiro:hasPermission="vote:vote:add">
                <i class="fa fa-plus"></i> 添加
            </a>
            <!--<a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()"-->
               <!--shiro:hasPermission="vote:vote:remove">-->
                <!--<i class="fa fa-remove"></i> 删除-->
            <!--</a>-->
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('vote:vote:edit')}]];
    var removeFlag = [[${@permission.hasPermi('vote:vote:remove')}]];
    var datas = [[${@dict.getType('tb_vote_status')}]];
    var voteStatus = [[${@dict.getType('tb_select_status')}]];
    var prefix = ctx + "vote/voteChange";


    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "选举任务",
            showToggle: false,
            showColumns: false,
            detailView: true,
            sortName: "createTime",
            sortOrder: "desc",
            onExpandRow: function (index, row, $detail) {
                initChildTable(index, row, $detail);
            },
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '主键id',
                    visible: false
                },
                {
                    field: 'title',
                    title: '标题',
                    width: 120
                },
                {
                    field: 'status',
                    title: '发布状态',
                    width: 100,
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(datas, value);
                    }
                },
                {
                    field: 'remark',
                    title: '备注',
                    width: 80
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    width: 100,
                    sortable: true
                },
                {
                    title: '操作',
                    align: 'center',
                    width: 150,
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>查看详情</a> ');
                        if(row.status=='0'){
                            actions.push('<a class="btn btn-primary btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="start(\'' + row.id + '\')"><i class="fa fa-edit"></i>开始选举</a> ');
                            actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        } else if(row.status=='1'){
                            actions.push('<a class="btn btn-primary btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="end(\'' + row.id + '\')"><i class="fa fa-edit"></i>结束选举</a> ');
                        }


                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });


    initChildTable = function (index, row, $detail) {
         $detail.html('<div class="row"><div class="col-sm-6"><table id="table1" style="table-layout:fixed"></table></div> <div class="col-sm-6"><table id="table2" style="table-layout:fixed"></table></div> </div>').find('table');


        $("#table1").bootstrapTable({
            url: ctx + "vote/userby/list?type=2",
            method: 'post',
            sidePagination: "server",
            contentType: "application/x-www-form-urlencoded",
            showFooter: true,
            queryParams: {
                voteId: row.id
            },
            columns: [
                {
                    field: 'userName',
                    title: '被选举人姓名',
                    align:'center'
                },
                {
                    field: 'voteId',
                    title: '选举任务id',
                    visible:false
                },
                {
                    field: 'voteCount',
                    title: '票数',
                    footerFormatter: function (value) {
                        var sum = 0;
                        if (value.length > 0) {
                            for (var i in value) {
                                sum += parseFloat(value[i].voteCount);
                            }
                            return "票数总计：" + sum;
                        }
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="detail(\'' + row.id + '\',\'' + row.voteId + '\')"><i class="fa fa-edit"></i>详情</a> ');
                        return actions.join('');
                    }
                }
            ]
        });

        $("#table2").bootstrapTable({
            url: ctx + "vote/voteuser/list?type=2",
            method: 'post',
            sidePagination: "server",
            contentType: "application/x-www-form-urlencoded",
            queryParams: {
                voteId: row.id
            },
            columns: [
                {
                    field: 'id',
                    title: '主键id',
                    visible: false
                },
                {
                    field: 'userName',
                    title: '选举人姓名',
                    align:'center'
                },
                {
                    field: 'voteId',
                    title: '选举任务id',
                    visible:false
                },
                {
                    field: 'status',
                    title: '投票状态',
                    align:'center',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(voteStatus, value);
                    }
                }
                ]
        });
    };

    function start(id) {
        $.modal.confirm("确认开始选举吗？", function () {
            var url = prefix + "/edit";
            var data = {
                "id": id ,
                "status":"1"};
            $.operate.submit(url, "post", "json", data);
        })
    }

    function end(id) {
        $.modal.confirm("确认结束选举吗？", function () {
            var url = prefix + "/edit";
            var data = {
                "id": id ,
                "status":"2"};
            $.operate.submit(url, "post", "json", data);
        })
    }
    
    function detail(id,voteId) {
        var url = prefix +"/detail?voteUserById="+id+ "&voteId=" + voteId;
        layer.open({
            type: 2,
            title:"详情",
            area: ['800px', '700px'],
            fix: false,
            //不固定
            maxmin: true,
            shade: 0.3,
            content: url,
            btn: ['关闭'],
            // 弹层外区域关闭
            shadeClose: true,
            yes: function (index, layero) {
                layer.close(index);
            }
        });
    }

</script>
</body>
</html>